//
//  ElasticBoxView.m
//  TST
//
//  Created by POP3mac on 2020/4/28.
//  Copyright © 2020 YTYanK. All rights reserved.
//

#import "ElasticBoxView.h"
#import "ZFDataTableView.h"

@interface ElasticBoxView()<ZFScrollDelegate>
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UILabel *numLabel;
@property (strong, nonatomic) UILabel *customerLabel;
@property (strong, nonatomic) UILabel *contactsLabel;
//联系人电话
@property (strong, nonatomic) UILabel *phoneLabel;
@property (strong, nonatomic) UILabel *addressLabel;
//工作内容
@property (strong, nonatomic) UILabel *contentLabel;
//参与人
@property (strong, nonatomic) UILabel *playersLabel;
//负责人
@property (strong, nonatomic) UILabel *dutyPeopleLabel;
//开始时间
@property (strong, nonatomic) UILabel *startLabel;
//结束时间
@property (strong, nonatomic) UILabel *endLabel;

/* 表格部分 */

@property (strong, nonatomic)YTYLabel *name;
//库存
@property (strong, nonatomic)YTYLabel *stock;
@property (strong, nonatomic)YTYLabel *number;
@property (strong, nonatomic)YTYLabel *nameValue;
@property (strong, nonatomic)YTYLabel *stockValue;
@property (strong, nonatomic)YTYLabel *numberValue;

@property (strong, nonatomic)UIButton  *button;

@property (strong, nonatomic)UIView * subview;

@property (nonatomic, strong) UIScrollView *scroll;
/** x方向数据*/
@property (nonatomic, strong) NSArray *arrX;
/** y方向数据*/
@property (nonatomic, strong) NSArray *arrY;
@property (nonatomic, strong) UIView *headView;
@end


@implementation ElasticBoxView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self  initAllView];
    }
    return self;
}

-(void)initAllView {
    [self setAllInitSubView];
    [self setAllSubViewLayout];
    [self updateAllData];
}
//#define ScreenWidth [UIScreen mainScreen].bounds.size.width
//#define ScreenHeight [UIScreen mainScreen].bounds.size.height
//#define ItemWidth SCREEN_WIDTH/5
#define ItemHeight 44
#define ScrollHeight (ItemHeight*3)
- (void)setAllInitSubView {
    self.subview = [[UIView alloc] initWithFrame:CGRectZero];
    self.subview.backgroundColor = UIColor.whiteColor;
    [self addSubview:self.subview];
    
    CGFloat maxw = (SCREEN_WIDTH * 0.8) * 0.9;

    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.lineBreakMode = NSLineBreakByClipping;
    self.titleLabel.preferredMaxLayoutWidth = maxw;
    self.titleLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    [self addSubview:self.titleLabel];

    self.numLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    self.numLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    [self addSubview:self.numLabel];

    self.customerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    self.customerLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    [self addSubview:self.customerLabel];

    self.contactsLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    self.contactsLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    [self addSubview:self.contactsLabel];

    self.phoneLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    self.phoneLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    [self addSubview:self.phoneLabel];


    self.addressLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    self.addressLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    [self addSubview:self.addressLabel];

    self.contentLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.lineBreakMode = NSLineBreakByClipping;
    self.contentLabel.preferredMaxLayoutWidth = maxw;
    self.contentLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    [self addSubview:self.contentLabel];
    
    
    // 表格部分
    self.name = [[YTYLabel alloc] initWithFrame:CGRectZero];
    self.name.textInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    self.name.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    [self.name setViewBorderCornerRadius:0 borderWidth:1 borderColor:UIColor.grayColor];
    [self addSubview:self.name];

    self.stock = [[YTYLabel alloc] initWithFrame:CGRectZero];
    self.stock.textInsets = UIEdgeInsetsMake(0, 10, 0, 0); //UIEdgeInsets
    self.stock.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    [self.stock setViewBorderCornerRadius:0 borderWidth:1 borderColor:UIColor.grayColor];
    [self addSubview:self.stock];

    self.number = [[YTYLabel alloc] initWithFrame:CGRectZero];
    self.number.textInsets = UIEdgeInsetsMake(0, 10, 0, 0); //UIEdgeInsets
    self.number.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    [self.number setViewBorderCornerRadius:0 borderWidth:1 borderColor:UIColor.grayColor];
    [self addSubview:self.number];
    
    
    self.nameValue = [[YTYLabel alloc] initWithFrame:CGRectZero];
    self.nameValue.numberOfLines = 0;
    self.nameValue.preferredMaxLayoutWidth = maxw;
    self.nameValue.lineBreakMode = NSLineBreakByClipping;
    self.nameValue.textInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    self.nameValue.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    [self.nameValue setViewBorderCornerRadius:0 borderWidth:1 borderColor:UIColor.grayColor];
    [self addSubview:self.nameValue];

    self.stockValue = [[YTYLabel alloc] initWithFrame:CGRectZero];
    self.stockValue.textInsets = UIEdgeInsetsMake(0, 10, 0, 0); //UIEdgeInsets
    self.stockValue.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    [self.stockValue setViewBorderCornerRadius:0 borderWidth:1 borderColor:UIColor.grayColor];
    [self addSubview:self.stockValue];

    self.numberValue = [[YTYLabel alloc] initWithFrame:CGRectZero];
    self.numberValue.textInsets = UIEdgeInsetsMake(0, 10, 0, 0); //UIEdgeInsets
    self.numberValue.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    [self.numberValue setViewBorderCornerRadius:0 borderWidth:1 borderColor:UIColor.grayColor];
    [self addSubview:self.numberValue];
     
    
    {
        //参与人
        self.playersLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.playersLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
        [self addSubview:self.playersLabel];
        //负责人
        self.dutyPeopleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.dutyPeopleLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
        [self addSubview:self.dutyPeopleLabel];

        //开始时间
        self.startLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.startLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
        [self addSubview:self.startLabel];

        //结束时间
        self.endLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.endLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
        [self addSubview:self.endLabel];
    }
    
    
    self.button = [[UIButton alloc] initWithFrame:CGRectZero];
    self.button.backgroundColor = UIColor.whiteColor;
    self.button.titleLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    [self.button setTitleColor:YTYRGBA(0, 0, 1, 0.9) forState:UIControlStateNormal];
    [self addSubview:self.button];
    
    
    

    
    
    
}

- (void)setAllSubViewLayout {
   [self.subview mas_makeConstraints:^(MASConstraintMaker *make) {
                  make.centerY.equalTo(self);
                  make.centerX.equalTo(self);
                  make.height.equalTo(self).with.multipliedBy(0.9);
                  make.width.equalTo(self).with.multipliedBy(0.9);
   }];
    
    // 部分
    {
    
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.subview).with.offset(10);
            make.centerX.equalTo(self.subview);
            make.height.mas_equalTo(45);
            make.width.equalTo(self.subview).with.multipliedBy(0.9);
        }];
        [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom).with.offset(10);
            make.centerX.equalTo(self.subview);
            make.height.mas_equalTo(30);
            make.width.equalTo(self.subview).with.multipliedBy(0.9);
        }];

        [self.customerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.numLabel.mas_bottom).with.offset(10);
            make.centerX.equalTo(self.subview);
            make.height.mas_equalTo(30);
            make.width.equalTo(self.subview).with.multipliedBy(0.9);
        }];
        //联系人
        [self.contactsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.customerLabel.mas_bottom).with.offset(10);
            make.centerX.equalTo(self.subview);
            make.height.mas_equalTo(30);
            make.width.equalTo(self.subview).with.multipliedBy(0.9);
        }];

        [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contactsLabel.mas_bottom).with.offset(10);
            make.centerX.equalTo(self.subview);
            make.height.mas_equalTo(30);
            make.width.equalTo(self.subview).with.multipliedBy(0.9);
        }];
        [self.addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.phoneLabel.mas_bottom).with.offset(10);
            make.centerX.equalTo(self.subview);
            make.height.mas_equalTo(30);
            make.width.equalTo(self.subview).with.multipliedBy(0.9);
        }];
        [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.addressLabel.mas_bottom).with.offset(10);
            make.centerX.equalTo(self.subview);
            make.height.mas_equalTo(50);
            make.width.equalTo(self.subview).with.multipliedBy(0.9);
        }];
        
    }
    
    // 表格
    CGFloat w = ((SCREEN_WIDTH * 0.9) - 40) /3;
    {
        [self.name mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentLabel.mas_bottom).with.offset(10);
            make.left.equalTo(self.subview).with.offset(20);
            make.height.mas_equalTo(30);
            make.width.mas_equalTo(w);
        }];
        [self.stock mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentLabel.mas_bottom).with.offset(10);
            make.left.equalTo(self.name.mas_right).with.offset(-1);
            make.height.mas_equalTo(30);
            make.width.mas_equalTo(w);
        }];
        [self.number mas_makeConstraints:^(MASConstraintMaker *make) {
             make.top.equalTo(self.contentLabel.mas_bottom).with.offset(10);
             make.left.equalTo(self.stock.mas_right).with.offset(-1);
             make.height.mas_equalTo(30);
             make.width.mas_equalTo(w);
        }];
        [self.nameValue mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.name.mas_bottom).with.offset(-1);
            make.left.equalTo(self.subview).with.offset(20);
            make.height.mas_equalTo(50);
            make.width.mas_equalTo(w);
        }];
        [self.stockValue mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.name.mas_bottom).with.offset(-1);
            make.left.equalTo(self.name.mas_right).with.offset(-1);
            make.height.mas_equalTo(50);
            make.width.mas_equalTo(w);
        }];
        [self.numberValue mas_makeConstraints:^(MASConstraintMaker *make) {
             make.top.equalTo(self.name.mas_bottom).with.offset(-1);
             make.left.equalTo(self.stock.mas_right).with.offset(-1);
             make.height.mas_equalTo(50);
             make.width.mas_equalTo(w);
        }];
    }
    
//     {
//                NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
//                NSData *data = [NSData dataWithContentsOfFile:path];
//                NSDictionary *rootDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
//
//                NSDictionary *dataDic = [rootDict objectForKey:@"data"];
//
//                //x方向数据
//                _arrX = [dataDic objectForKey:@"table_titles"];
//
//
//                //y方向数据
//               NSArray<ProductsModel*> *ary  = [TSTApi sharedTSTApi].jobsViewModel.products;
//                ProductsModel *p = [ProductsModel yy_modelWithDictionary:ary[0]];  //  ary[0];
//   //             _arrY =   //[dataDic objectForKey:@"list_datas"];
//
//
//                CGFloat W = ((SCREEN_WIDTH * 0.8) - 40);
//                CGFloat itemW = W/_arrX.count;
//                for (int i = 0; i < _arrX.count; i++) {
//                      ZFDataTableView *tableView = [[ZFDataTableView alloc] initWithFrame:CGRectMake((itemW*i ) + 20, 320, itemW, ScrollHeight) style:UITableViewStylePlain];
//
//                      //x方向 取出key对应的字符串名字
//                      NSString *xkey = [NSString stringWithFormat:@"name%d",i+1];
//                      NSString *xname = [_arrX[i] objectForKey:xkey];
//                      tableView.headerStr = xname;
//
//                      //y方向
//                      NSMutableArray *titleArr2 = [NSMutableArray array];
//                      for (int j=0; j< ary.count; j++) {
//   //                       NSString *ykey = [NSString stringWithFormat:@"date%d",i+1];
//   //                       NSString * ykey = [NSString stringWithFormat:@""];
//   //                       NSString *yname = [_arrY[j] objectForKey:ykey];
//                          NSString *yname = @"";
//                          if (i == 0) {
//                               yname = [NSString stringWithFormat:@"%@",p.product_name];
//                          }else if(i == 1) {
//                              yname = [NSString stringWithFormat:@"%.2f",p.inventory];
//                          }else {
//                             yname = [NSString stringWithFormat:@"%.2f",p.quantity];
//                          }
//
//                          [titleArr2 addObject:yname];
//                      }
//                      tableView.titleArr = titleArr2;
//                      [tableView reloadData];
//            //          tableView.scroll_delegate = self;
//                      [self.subview addSubview:tableView];
//
//
//                  }
//
//          }
//
//
//
    
    
    // 參與人
    {
    
       [self.playersLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.numberValue.mas_bottom).with.offset(10);
            make.centerX.equalTo(self.subview);
            make.height.mas_equalTo(30);
            make.width.equalTo(self.subview).with.multipliedBy(0.9);
       }];
        
        [self.dutyPeopleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
             make.top.equalTo(self.playersLabel.mas_bottom).with.offset(10);
             make.centerX.equalTo(self.subview);
             make.height.mas_equalTo(30);
             make.width.equalTo(self.subview).with.multipliedBy(0.9);
        }];
        
        [self.startLabel mas_makeConstraints:^(MASConstraintMaker *make) {
             make.top.equalTo(self.dutyPeopleLabel.mas_bottom).with.offset(10);
             make.centerX.equalTo(self.subview);
             make.height.mas_equalTo(30);
             make.width.equalTo(self.subview).with.multipliedBy(0.9);
        }];
        
        [self.endLabel mas_makeConstraints:^(MASConstraintMaker *make) {
             make.top.equalTo(self.startLabel.mas_bottom).with.offset(10);
             make.centerX.equalTo(self.subview);
             make.height.mas_equalTo(30);
             make.width.equalTo(self.subview).with.multipliedBy(0.9);
        }];
    }
    
    
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
               make.bottom.equalTo(self.subview.mas_bottom).with.offset(- 10);
               make.right.equalTo(self.subview.mas_right).with.offset(-10);
               make.height.mas_equalTo(30);
               make.width.equalTo(self.subview).with.multipliedBy(0.2);
    }];
    
    
    
    
    
    
}


- (void)updateAllData {
    
    self.titleLabel.text =[NSString stringWithFormat:@"標題: %@",[TSTApi sharedTSTApi].jobsViewModel.job_title];  //@": Hammer Tapping - 8-10 Ngai TsinLong Road";
    self.numLabel.text = [NSString stringWithFormat:@"编号: %@",[TSTApi sharedTSTApi].jobsViewModel.number];
    self.customerLabel.text =  [NSString stringWithFormat:@"客户: %@",[TSTApi sharedTSTApi].jobsViewModel.customer_name]; //
    
    self.contactsLabel.text =  [NSString stringWithFormat:@"联系人: %@",[TSTApi sharedTSTApi].jobsViewModel.contacts_name];
    self.phoneLabel.text =  [NSString stringWithFormat:@"联系人电话: %@",[TSTApi sharedTSTApi].jobsViewModel.contacts_phone];
    self.addressLabel.text = [NSString stringWithFormat:@"地址: %@",[TSTApi sharedTSTApi].jobsViewModel.address];
    
    self.contentLabel.text =  [NSString stringWithFormat:@"工作内容: %@",[TSTApi sharedTSTApi].jobsViewModel.content];
    
    
    
    self.name.text = @"项目名称";
    self.stock.text = @"库存";
    self.number.text = @"数量";
    NSArray<ProductsModel*> *ary  = [TSTApi sharedTSTApi].jobsViewModel.products;
    if(ary.count < 1) {
       self.nameValue.text = [NSString stringWithFormat:@"---"];
       self.stockValue.text = [NSString stringWithFormat:@"0.00"];
       self.numberValue.text = [NSString stringWithFormat:@"0.00"];
    }else {
        ProductsModel *p = [ProductsModel yy_modelWithDictionary:ary[0]];
           self.nameValue.text = [NSString stringWithFormat:@"%@",p.product_name];
           self.stockValue.text = [NSString stringWithFormat:@"%.2f",p.inventory];
           self.numberValue.text = [NSString stringWithFormat:@"%.2f",p.quantity];
        
    }

    
    self.playersLabel.text =  [NSString stringWithFormat:@"參與人: %@",[TSTApi sharedTSTApi].jobsViewModel.participants];
    self.dutyPeopleLabel.text = [NSString stringWithFormat:@"責任人: %@",[TSTApi sharedTSTApi].jobsViewModel.responsible_name];
    self.startLabel.text = [NSString stringWithFormat:@"开始時間: %@",[TSTApi sharedTSTApi].jobsViewModel.start_time];
    self.endLabel.text =  [NSString stringWithFormat:@"結束時間: %@",[TSTApi sharedTSTApi].jobsViewModel.end_time];
    
//    [self.button setTitle:@"確認" forState:UIControlStateNormal];
//    [self.button addTarget:self action:@selector(<#selector#>) forControlEvents:UIControlEventTouchUpInside];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
#pragma mark - ZFScrollDelegate

- (void)dataTableViewContentOffSet:(CGPoint)contentOffSet {
    for (UIView *subView in _scroll.subviews) {
        if ([subView isKindOfClass:[ZFDataTableView class]]) {
            [(ZFDataTableView *)subView setTableViewContentOffSet:contentOffSet];
        }
    }
    
    for (UIView *subView in _headView.subviews) {
        [(ZFDataTableView *)subView setTableViewContentOffSet:contentOffSet];
    }
}

#pragma mark - UIScrollViewDelegate

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    CGPoint p = scrollView.contentOffset;
//    NSLog(@"%@",NSStringFromCGPoint(p));
//}

//- (void)didReceiveMemoryWarning {
//    [super didReceiveMemoryWarning];
//    // Dispose of any resources that can be recreated.
//}
@end
